﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using com.code.Editors.Code;

namespace com.Editors
{
    public partial class TableDataForm : Form
    {
         
        ArrayList tables = null;
        IServiceProvider provider = null;
        IDictionaryService dictionaryService = null;
        private ArrayList colObjArr = new ArrayList();

        public TableDataForm()
        {
            InitializeComponent();        
        }

        public TableDataForm(IServiceProvider provider)
        {
            InitializeComponent();
            this.provider = provider;
        }

        public void setDataTables(ArrayList list)
        {
            tables = list;
            foreach (GuidDataTable table in tables)
                this.setTabs(table);
        }

        private void setTabs(GuidDataTable table)
        {
            TabPage page = new TabPage(table.TableName);
            this.SuspendLayout();
            FlowLayoutPanel pagePanel = new FlowLayoutPanel();
            pagePanel.FlowDirection = FlowDirection.LeftToRight;
            pagePanel.Width = 714;
            pagePanel.HorizontalScroll.Maximum = 0;
            //pagePanel.VerticalScroll.Minimum = 487;
            pagePanel.AutoScroll = true;
            pagePanel.Dock = DockStyle.Fill; 

            SchemaDAL obj = new SchemaDAL(DataForm.GetConStr());
            foreach (GuidDataColumn col in obj.GetTableColumns(table.TableName))
            {
                com.Editors.UserControls.ColumnCode colObj = 
                                    new com.Editors.UserControls.ColumnCode(col, provider);
                table.addGuidDataColumn(col);
                colObjArr.Add(colObj);
                pagePanel.Controls.Add(colObj);
            }
                //pagePanel.Controls.Add(addNewColumnRow(col));
            page.Controls.Add(pagePanel);
            tabControl1.TabPages.Add(page);
            this.ResumeLayout();
        }

        public void addOutputTabs(string caption , string content)
        {
            TabPage page = new TabPage(caption);
            this.SuspendLayout();
            FlowLayoutPanel pagePanel = new FlowLayoutPanel();            
            pagePanel.AutoScroll = true;
            pagePanel.Dock = DockStyle.Fill;

            RichTextBox txt = new RichTextBox();
            txt.Name = "txt" + caption;
            txt.Size = new Size(708, 481);
            txt.Text = content;

            //this.richTextBox1.Text = content;
            pagePanel.Controls.Add(txt);
            page.Controls.Add(pagePanel);
            tabControl1.TabPages.Add(page);
            this.ResumeLayout();
        }
        private FlowLayoutPanel addNewColumnRow(string colName)
        {
            System.Windows.Forms.FlowLayoutPanel panel;
            System.Windows.Forms.Label lblColName;
            System.Windows.Forms.CheckBox chkCol;
            System.Windows.Forms.CheckBox chkColIsDetail;
            System.Windows.Forms.ComboBox cmbCol;
            // panel
            //
            panel = new System.Windows.Forms.FlowLayoutPanel();
            chkCol = new System.Windows.Forms.CheckBox();
            chkColIsDetail = new System.Windows.Forms.CheckBox();
            lblColName = new System.Windows.Forms.Label();
            cmbCol = new ComboBox();
            
            panel.Name = "panel"+colName;
            panel.Size = new System.Drawing.Size(900, 20);
            panel.AutoScroll = true;
            panel.Location = new System.Drawing.Point(3, 20);
            
            //this.t1Panel1.TabIndex = 0;
            // 
            // chkT1Panel1
            // 
            chkCol.AutoSize = true;
            chkCol.Name = "chk" + colName;
            chkCol.Text = "";
            chkCol.UseVisualStyleBackColor = true;
            panel.Controls.Add(chkCol);
            // 
            // t1Panel1lblColName
            //
            //lblColName.Location.X
            lblColName.AutoSize = true;
            lblColName.Name = "lbl" + colName;
            lblColName.Size = new System.Drawing.Size(35, 13);
            //lblColName.Location = new System.Drawing.Point(x+10, width);
            lblColName.Text = "" + colName;
            panel.Controls.Add(lblColName);

            this.fillControlType(cmbCol);
            panel.Controls.Add(cmbCol);

            chkCol.AutoSize = true;
            chkCol.Name = "chkDetail" + colName;
            chkCol.Text = "";
            chkCol.UseVisualStyleBackColor = true;
            panel.Controls.Add(chkColIsDetail);

            t1Panel1.Controls.Add(panel);

          

            return panel;
        }


        private void fillControlType(ComboBox cmb)
        {
            cmb.Items.Add("TextBox");
            cmb.Items.Add("DropDownList");
            cmb.Items.Add("CheckBox");
            cmb.Items.Add("RadioButton");
            cmb.Items.Add("HyperLink");
            cmb.Items.Add("Label");
            cmb.Items.Add("HtmlHidden");
            cmb.Items.Add("TextBox");
            cmb.Items.Add("TextBox");
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            ArrayList tmpDataCols = new ArrayList();
            foreach (com.Editors.UserControls.ColumnCode colObj in colObjArr)
            {
                tmpDataCols.Add(colObj.DataColObj);
            }

            UITableRelation obj = new UITableRelation(this.provider);
            obj.setUITableRelation(tables);
            obj.Show();
            
            dictionaryService = provider.GetService(typeof(IDictionaryService)) as IDictionaryService;

            //dictionaryService.SetValue("ClassName", dictionaryService.GetValue(""));
            dictionaryService.SetValue("DataStr", tables);
            //dictionaryService.SetValue("TargetDALNamespace", provider. );

            //this.Close();
        }

    }
}
